// vptr.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

template <typename T>
struct Node
{
        T data;
        Node<T> *next;
};

template <typename T>
class LinkStack
{
public:

    
    LinkStack(int size);
    ~LinkStack();
    
    bool isEmpty();
    bool isFull();
    int getCount();
    int getSize();
    T top();
    T pop();
    bool push(T data);

private:
    Node<T> *ptop;
    int size;
    int count;
};

template <typename T>
LinkStack<T>::LinkStack(int size)
{
    count = 0;
    this->size = size;
    ptop = NULL; 
}

template <typename T>
LinkStack<T>::~LinkStack()
{
    if(ptop)
        delete ptop;
        ptop = NULL;
}

template <typename T>
int LinkStack<T>::getCount()
{
    return count;
}

template <typename T>
int LinkStack<T>::getSize()
{
    return size;
}

template <typename T>
bool LinkStack<T>::isEmpty()
{
    return count == 0;
}

template <typename T>
bool LinkStack<T>::isFull()
{
    return count == size;
}

template <typename T>
T LinkStack<T>::top()
{
    if(!ptop)
        return (T)-1;
    return ptop->data;
}

template <typename T>
T LinkStack<T>::pop()
{
    if ((!ptop) || (count == 0))
        return (T)-1;
    count--;
	T d = ptop->data;
	Node<T> *tmp = ptop;
	ptop = ptop->next;

    return d;
}

template <typename T>
bool LinkStack<T>::push(T data)
{
    if(count == size)
        return false;
	Node<T> *newnode = new Node<T>();
    newnode->data = data;
    newnode->next = NULL;
    if(ptop)
	{
		newnode->next = ptop;
        ptop = newnode;
    }
    else
	{
        ptop = newnode;
    }
    count++;
    return true;
}


int main(void) {
    LinkStack<int> linkstack(10); ;
    for(int i=0; i<10; i++)
    {
        linkstack.push(i);
    }
    cout<<"size:"<<linkstack.getCount()<<endl;
    while(!linkstack.isEmpty())
    {
        printf("item = %d\n", linkstack.pop());
    }
	system("pause");
    return 0;
}